import iconv from 'iconv-lite';

export default class TextEncoding {
  static Options = {
    encoding: {
      type: 'menu',
      options: ['UTF-7', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'GBK', 'CP437', 'CP1252']
    }
  }

  options = {
    encoding: 'UTF-8',
    addBOM: false,
  }

  static Encoder = class Encoder extends TextEncoding {
    static Name = "<%= UIText.TextEncoding %>";

    static Options = {
      ...TextEncoding.Options,
      addBOM: {type: 'switch', label: 'Add BOM'},
    }

    compute(text) {
      return iconv.encode(text, this.options.encoding, this.options);
    }
  }

  static Decoder = class Decoder extends TextEncoding {
    static Name = "<%= UIText.TextDecoding %>";

    compute(data) {
      return iconv.decode(data, this.options.encoding);
    }
  }
}
